<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class AffiliesModel extends JLModelBase {

    /**
     * Elimina Affilies
     * @Retorna bool
     */
    public function deleteAffilies($id) {
        $_true = false;

        $sql = "DELETE FROM 
              `tbl_suscriptores` 
            WHERE 
              `int_id_suscriptor` = " . $this->DB->Quote($id) . "";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

    /**
     * seleciona todos los Affilies
     * @Retorna bool
     */
    public function getAllAffilies() {
        $_true = null;

        $sql = "SELECT 
            `int_id_suscriptor`,
            `var_fullname`,
            `var_email`,
            `var_code` 
            FROM `tbl_suscriptores`";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
        }

        return $_true;
    }

    function getAffilieByCode($code) {
        $sql = "SELECT 
        `int_id_suscriptor`,
        `var_fullname`,
        `var_email`,
        `var_code`
        FROM `tbl_suscriptores`
        WHERE var_code = '$code'";
        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
            $_true = $_true[0];
        }

        return $_true;
    }

    function getAffilieByLimit($limit, $sent) {
        $sql = "SELECT 
        `int_id_suscriptor`,
        `var_fullname`,
        `var_email`,
        `var_code`
        FROM `tbl_suscriptores`
        WHERE int_sent = $sent
        LIMIT $limit";
        
        $this->DB->executeQuery($sql);

        $_true = NULL;

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
        }

        return $_true;
    }
    
    function updateAffiliesByIds($ids, $sent) {
        $sql = "UPDATE tbl_suscriptores
        SET `int_sent` = $sent
        WHERE int_id_suscriptor IN(". implode(',', $ids) .")";
        
        $this->DB->executeQuery($sql);

        $_true = NULL;

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
        }

        return $_true;
    }
    
    function updateAffiliesSent($sent) {
        $sql = "UPDATE tbl_suscriptores
        SET `int_sent` = $sent
        WHERE int_sent <> $sent";
        
        $this->DB->executeQuery($sql);
        
        return $this->DB->getRowsAffected();
    }
}